package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class TeachplanServiceImpl implements TeachplanService {
    @Autowired
    private TeachplanMapper teachplanMapper;

    @Autowired
    private TeachplanMediaMapper teachplanMediaMapper;

    @Override
    public List<TeachplanDto> findTeachplanTree(Long courseId) {
        return teachplanMapper.selectTreeNodes(courseId);
    }
    private int getTeachplanCount(Long courseId,Long parentId){
        QueryWrapper<Teachplan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        queryWrapper.eq("parentid",parentId);
        Integer count = teachplanMapper.selectCount(queryWrapper);
        return count+1;
    }

    @Override
    public void saveTeachplan(SaveTeachplanDto teachplan) {
        //通过课程计划id判断是新增还是修改
        Long id = teachplan.getId();
        if(id == null){
            //新增
            Teachplan teachplan1 = new Teachplan();
            BeanUtils.copyProperties(teachplan,teachplan1);
            //确定排序字段,找到它的同级节点个数,排序字段就是个数+1
            //select count(1) from teachplan where course_id=117 and parentid=268
            Long parentid = teachplan1.getParentid();
            Long courseId = teachplan1.getCourseId();
            int count = getTeachplanCount(courseId,parentid);
            teachplan1.setOrderby(count);

            teachplanMapper.insert(teachplan1);
        }else{
            //修改
            Teachplan teachplan1 = teachplanMapper.selectById(id);
            BeanUtils.copyProperties(teachplan,teachplan1);
            teachplanMapper.updateById(teachplan1);
        }

    }

    @Transactional
    /**
     * 根据ID删除课程计划
     * @param teachplanId
     */
    public void deleteTeachplan(Long teachplanId) {
        if(teachplanId==null){
            XueChengPlusException.cast("课程计划为空");
        }
        //这里也可以用grade来判断
        LambdaQueryWrapper<Teachplan>queryWrapper = new LambdaQueryWrapper<>();
        //select * from teachplan where parenid = {当前章计划的id}
        queryWrapper.eq(Teachplan::getParentid,teachplanId);
        //获取一下查询的条目数
        Integer count = teachplanMapper.selectCount(queryWrapper);
        //如果当前课程下有小节,则抛出异常
        if(count > 0){
            XueChengPlusException.cast("课程计划信息还有子级信息,无法操作");
        }else{
            //课程计划下无小节,直接删除该课程计划和对应的媒资信息
            teachplanMapper.deleteById(teachplanId);
            //条件构造器
            LambdaQueryWrapper<TeachplanMedia> mediaLambdaQueryWrapper = new LambdaQueryWrapper<>();
            //删除媒资信息中对应teachplanId的数据
            mediaLambdaQueryWrapper.eq(TeachplanMedia::getTeachplanId,teachplanId);
            teachplanMediaMapper.delete(mediaLambdaQueryWrapper);
        }
       //删除第一节别的大章节时要求大章节下边没有小章节方可删除
        //删除第二级别的小章节的同时需要将teachplan_media表关联的信息也删除
    }

    /**
     * 课程计划排序
     * @param moveType
     * @param teachplanId
     */
    @Transactional
    @Override
    public void orderByTeachplan(String moveType, Long teachplanId) {
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        //获取当前层级和当前orderby,章节移动和小节移动的处理方式不同
        Integer grade = teachplan.getGrade();
        Integer orderby = teachplan.getOrderby();
        //章节移动是比较同一课程id下的orderby
        Long courseId = teachplan.getCourseId();
        //小节移动是比较同一章节id下的orderby
        Long parentid = teachplan.getParentid();
        if("moveup".equals(moveType)){
            if(grade==1){
                //章节上移,找到上一个章节orderby,然后与其交换orderby
                //SELECT * FROM teachplan WHERE courseId=117 AND grade=1 AND orderby<? ORDER BY orderby DESC LIMIT 1
                LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(Teachplan::getCourseId,courseId)
                        .eq(Teachplan::getGrade,1)
                        .lt(Teachplan::getOrderby,orderby)
                        .orderByDesc(Teachplan::getOrderby)
                        .last("LIMIT 1");
                Teachplan tmp = teachplanMapper.selectOne(queryWrapper);
                exchangeOrderby(teachplan,tmp);

            }else if(grade==2){
                //小节上移
                //SELECT *FROM teachplan WHERE parentId = 268 AND orderby<5 ORDER BY orderby DESC LIMIT 1
                LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(Teachplan::getParentid,parentid)
                        .lt(Teachplan::getOrderby,orderby)
                        .orderByDesc(Teachplan::getOrderby)
                        .last("LIMIT 1");
                Teachplan tmp = teachplanMapper.selectOne(queryWrapper);
                exchangeOrderby(teachplan,tmp);
            }
        }else if("movedown".equals(moveType)){
            if(grade==1){
                //章节下移
                LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(Teachplan::getCourseId,courseId)
                        .eq(Teachplan::getGrade,1)
                        .gt(Teachplan::getOrderby,orderby)
                        .orderByAsc(Teachplan::getOrderby)
                        .last("LIMIT 1");
                Teachplan tmp = teachplanMapper.selectOne(queryWrapper);
                exchangeOrderby(teachplan,tmp);
            }else if(grade==2){
                //小节下移
                LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(Teachplan::getParentid,parentid)
                        .gt(Teachplan::getOrderby,orderby)
                        .orderByAsc(Teachplan::getOrderby)
                        .last("LIMIT 1");
                Teachplan tmp = teachplanMapper.selectOne(queryWrapper);
                exchangeOrderby(teachplan,tmp);
            }

        }

    }

    /**
     * 交换两个Teachplan的orderby
     *
     */
    private void exchangeOrderby(Teachplan teachplan,Teachplan tmp){
        if(tmp==null){
            //也就是上面查询出来发现没有 就是找不到
            XueChengPlusException.cast("已经到头啦,不能再移啦");
        }else{
            //交换orderby,更新
            Integer orderby = teachplan.getOrderby();
            Integer tmpOrderby = tmp.getOrderby();
            teachplan.setOrderby(tmpOrderby);
            tmp.setOrderby(orderby);
            teachplanMapper.updateById(teachplan);
            teachplanMapper.updateById(tmp);
        }
    }
}















